package cn.iocoder.yudao.module.worker.controller.admin.clerklevel;

import cn.iocoder.yudao.module.worker.controller.admin.category.vo.CategoryRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.admin.clerklevel.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerklevel.ClerkLevelDO;
import cn.iocoder.yudao.module.worker.service.clerklevel.ClerkLevelService;

@Tag(name = "管理后台 - 店员等级")
@RestController
@RequestMapping("/worker/clerk-level")
@Validated
public class ClerkLevelController {

    @Resource
    private ClerkLevelService clerkLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建店员等级")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:create')")
    public CommonResult<Long> createClerkLevel(@Valid @RequestBody ClerkLevelSaveReqVO createReqVO) {
        return success(clerkLevelService.createClerkLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员等级")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:update')")
    public CommonResult<Boolean> updateClerkLevel(@Valid @RequestBody ClerkLevelSaveReqVO updateReqVO) {
        clerkLevelService.updateClerkLevel(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改等级状态")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:update')")
    public CommonResult<Boolean> updateClerkLevelStatus(@Valid @RequestBody ClerkLevelSaveReqVO reqVO) {
        clerkLevelService.updateClerkLevelStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @PutMapping("/save")
    @Operation(summary = "更新店员等级")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:update')")
    public CommonResult<Boolean> saveClerkLevelPrice(@Valid @RequestBody ClerkLevelSaveReqVO updateReqVO) {
        clerkLevelService.saveClerkLevelPrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:delete')")
    public CommonResult<Boolean> deleteClerkLevel(@RequestParam("id") Long id) {
        clerkLevelService.deleteClerkLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:query')")
    public CommonResult<ClerkLevelRespVO> getClerkLevel(@RequestParam("id") Long id) {
        ClerkLevelDO clerkLevel = clerkLevelService.getClerkLevel(id);
        return success(BeanUtils.toBean(clerkLevel, ClerkLevelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:query')")
    public CommonResult<List<CategoryRespVO>> getClerkLevelPrice(@RequestParam("id") Long id) {
        List<CategoryRespVO> list = clerkLevelService.getClerkLevelPrice(id);
        return success(BeanUtils.toBean(list, CategoryRespVO.class));
    }

    @GetMapping("/getClerkLevelList")
    @Operation(summary = "获得店员等级")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:query')")
    public CommonResult<List<ClerkLevelRespVO>> getClerkLevelList() {
        List<ClerkLevelDO> list = clerkLevelService.getClerkLevelList();
        return success(BeanUtils.toBean(list, ClerkLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员等级分页")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:query')")
    public CommonResult<PageResult<ClerkLevelRespVO>> getClerkLevelPage(@Valid ClerkLevelPageReqVO pageReqVO) {
        PageResult<ClerkLevelDO> pageResult = clerkLevelService.getClerkLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClerkLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员等级 Excel")
    @PreAuthorize("@ss.hasPermission('worker:clerk-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkLevelExcel(@Valid ClerkLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkLevelDO> list = clerkLevelService.getClerkLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员等级.xls", "数据", ClerkLevelRespVO.class,
                        BeanUtils.toBean(list, ClerkLevelRespVO.class));
    }

}